/*----------------------------------------------------------------------
* Copyright (C) 2013 - 2014: Sergey Smolyannikov <brainstream@yandex.com>
*
* This file is part of lxpamon.
*
* lxpamon is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* lxpamon is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with lxpamon. If not, see http://www.gnu.org/licenses/gpl-3.0.txt.
----------------------------------------------------------------------*/

#include "utils.h"

char * fixPrintfTemplatePlaceholderOccurences(char * pf_template, const char * allowed_formats, char replace_with)
{
    unsigned char is_prev_percm = 0;
    const char * expexted = allowed_formats;
    for(char * smb = pf_template; *smb != '\0'; ++smb)
    {
        if(*smb == '%')
        {
            is_prev_percm = is_prev_percm ? 0 : 1;
            continue;
        }
        if(!is_prev_percm)
        {
            continue;
        }
        is_prev_percm = 0;
        if(*smb == *expexted)
        {
            ++expexted;
        }
        else
        {
            *(smb - 1) = replace_with;
            *smb = replace_with;
        }
    }
    return pf_template;
}
